import Vue from 'vue';
import VueRouter from 'vue-router';
import store from '../store';
Vue.use(VueRouter);

const originalPush = VueRouter.prototype.push;
const originalReplace = VueRouter.prototype.replace;
VueRouter.prototype.push = function push(location, onResolve, onReject) {
    if (onResolve || onReject) return originalPush.call(this, location, onResolve, onReject);
    return originalPush.call(this, location).catch(err => err);
}
VueRouter.prototype.replace = function replace(location, onResolve, onReject) {
    if (onResolve || onReject) return originalReplace.call(this, location, onResolve, onReject);
    return originalReplace.call(this, location).catch(err => err);
}

import Placeholder from '../components/Layout/Placeholder.vue';
import Home from '../views/Home/Index.vue';
import About from '../views/About/Index.vue';
import Course from '../views/Course/Index.vue';
import News from '../views/News/Index.vue';
import NewsDetail from '../views/News/Detail.vue';
import Teacher from '../views/Teacher/Index.vue';
import Ascendant from '../views/Ascendant/Index.vue';
import Service from '../views/Service/Index.vue';

const routes = [
    {
        // 匹配到的子组件，会被添加到父组件的router-view中
        // 如果父组件不提供路由出口，则子组件不能被加载
        path: '/', component: Placeholder,
        children: [
            // 新建一个home路由，默认路由重定向到该路由
            // 解决：tabbar首页不能点击切换的问题
            { path: '', redirect: '/home' },
            { path: 'home', name: 'home', component: Home },
            { path: 'about', name: 'about', component: About },
            { path: 'course', name: 'course', component: Course },
            { path: 'news', name: 'news', component: News },
            { path: 'newsDetail/:id', name: 'newsDetail', component: NewsDetail },
            { path: 'teacher', name: 'teacher', component: Teacher },
            { path: 'ascendant', name: 'ascendant', component: Ascendant },
            { path: 'service', name: 'service', component: Service },
        ]
    }
]
const router = new VueRouter({
    mode: 'history',
    base: process.env.BASE_URL,
    routes
});

router.afterEach((to, from, next) => {
    window.scrollTo(0,0);
});

export default router